2025.3.18 戻り値の型指定
Pythonの関数で戻り値の型を指定するには、関数定義の先頭行に「->」を記述する。
code:python
def func() -> int:
return 1
result = func()
print('# 結果:', type(result), result)
# 結果: <class 'int'> 1
しかしながら、Pythonの型の扱いは厳密で無いため、異なる値を返した場合でも処理が行われる。
code:python
def func() -> int:
return 'hoge'
result = func()
print('# 結果:', type(result), result)
# 結果: <class 'str'> hoge